package com.loooooo.pm2024.exception;

import com.loooooo.pm2024.data.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLIntegrityConstraintViolationException;

@ControllerAdvice
public class GlobalExceptionHandler {
    private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @Autowired
    HttpServletRequest request;

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleError3(Exception e) {
        if (isAjax(request)) {
            Result result = Result.error(10500, "处理异常", "");
            if (request != null) result.set("url", request.getRequestURI());
            return result;
        } else {
            ModelAndView model = new ModelAndView();
            model.addObject("message", e.getMessage());
            model.setViewName("/error/500");
            return model;
        }
    }


    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object handleError4(RuntimeException e) {
        e.printStackTrace();
        if (isAjax(request)) {
            String msg = e.getMessage();
            Result result = Result.error(10011, msg, "");
            if (request != null) result.set("url", request.getRequestURI());
            return result;
        } else {
            ModelAndView model = new ModelAndView();
            model.addObject("message", e.getMessage());
            model.setViewName("/error/500");
            return model;
        }
    }

    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseBody
    public Object handleError4(DuplicateKeyException e) {
        e.printStackTrace();
        if (isAjax(request)) {
            String msg = e.getMessage();
            Result result = Result.error(10011, "数据重复", e.getMessage());
            if (request != null) result.set("url", request.getRequestURI());
            return result;
        } else {
            ModelAndView model = new ModelAndView();
            model.addObject("message", "数据重复");
            model.setViewName("/error/500");
            model.getModel().put("url", request.getRequestURI());
            return model;
        }
    }

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    @ResponseBody
    public Object handleError54(SQLIntegrityConstraintViolationException e) {
        e.printStackTrace();
        if (isAjax(request)) {
            Result result = Result.error(10011, "数据重复", "");
            if (request != null) result.set("url", request.getRequestURI());
            return result;
        } else {
            ModelAndView model = new ModelAndView();
            model.setViewName("/error/500");
            model.getModel().put("url", request.getRequestURI());
            return model;
        }
    }

    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public Object handleError5(NullPointerException e) {
        e.printStackTrace();
        if (isAjax(request)) {
            Result result = Result.error(10011, "空指针异常", e.getMessage());
            if (request != null) result.set("url", request.getRequestURI());
            return result;
        } else {
            ModelAndView model = new ModelAndView();
            model.setViewName("/error/500");
            model.getModel().put("url", request.getRequestURI());
            return model;
        }
    }

    @ExceptionHandler(MyException.class)
    @ResponseBody
    public Object handleError6(MyException e) {
        if (isAjax(request)) {
            Result result = Result.error(10099, "服务异常", "");
            if (request != null) result.set("url", request.getRequestURI());
            return result;
        } else {
            ModelAndView model = new ModelAndView();
            model.setViewName("/error/500");
            model.getModel().put("url", request.getRequestURI());
            return model;
        }
    }

    static boolean isAjax(HttpServletRequest httpRequest) {
        return httpRequest.getHeader("Accept").contains(MediaType.APPLICATION_JSON_VALUE)
                || (httpRequest.getHeader("X-Requested-With") != null
                && "XMLHttpRequest"
                .equals(httpRequest.getHeader("X-Requested-With").toString()));
    }
}
